import { login } from '@/api/login'
import { setToken, removeToken } from '@/utils/token'

const state = {
  token: ''
}
const mutations = {
  SET_TOKEN(state, token) {
    state.token = token
  }
}
const actions = {
  login({ commit }, params) {
    return new Promise((resolve, reject) => {
      login(params).then(res => {
        if (res.code === 0) {
          commit('SET_TOKEN', res.token)
          setToken(res.token)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  loginOut({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
